package com.nondo.rdp.stock.service.impl;

import com.nondo.rdp.auth.domain.Role;
import com.nondo.rdp.auth.domain.User;
import com.nondo.rdp.auth.endpoint.model.Page;
import com.nondo.rdp.core.common.StatusConstant;
import com.nondo.rdp.stock.domain.SimulateBuy;
import com.nondo.rdp.stock.repository.SimulateBuyRepository;
import com.nondo.rdp.stock.service.SimulateBuyService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;

@Transactional
@Service
public class SimulateBuyServiceImpl implements SimulateBuyService {
    @Autowired
    SimulateBuyRepository simulateBuyRepository;

    @Override
    public SimulateBuy findOne(Long id) {
        return this.simulateBuyRepository.findOne(id);
    }

    @Override
    public SimulateBuy save(SimulateBuy simulateBuy) {
        return this.simulateBuyRepository.save(simulateBuy);
    }

    @Override
    public void update(SimulateBuy simulateBuy) {
        this.simulateBuyRepository.save(simulateBuy);
    }

    @Override
    public void delete(Long id) {
        this.simulateBuyRepository.deleteById(id);
    }

    @Override
    public Page<SimulateBuy> findAll(Page<SimulateBuy> page, SimulateBuy simulateBuy) {
        Sort sort = new Sort(Sort.Direction.DESC, "createDate");
        PageRequest pageRequest = new PageRequest(page.getPageNumber(), page.getPageSize(), sort);
        org.springframework.data.domain.Page<SimulateBuy> dictPage = simulateBuyRepository.findAll((root, query, builder) -> {

            List<Predicate> predicates = new ArrayList<>();
            //搜索股票代码
            if (StringUtils.isNotEmpty(simulateBuy.getCode())) {
                predicates.add(builder.like(root.get("code"), "%" + simulateBuy.getCode() + "%"));
            }
            //搜索ID
            if (simulateBuy.getId() != null && simulateBuy.getId() != 0) {
                predicates.add(builder.equal(root.get("id"), simulateBuy.getId()));
            }
            //搜索用户
            if (simulateBuy.getUser() != null) {
                predicates.add(builder.equal(root.get("user"), simulateBuy.getUser().getId()));
            }

            predicates.add(builder.like(root.get("delFlag"), Role.DEL_FLAG_NORMAL));
            Predicate[] predicatesArray = new Predicate[predicates.size()];
            predicates.toArray(predicatesArray);
            return builder.and(predicatesArray);

        }, pageRequest);

        return new Page<SimulateBuy>(dictPage);
    }

    @Override
    public SimulateBuy findByCode(String code) {
        return this.simulateBuyRepository.findByCodeAndDelFlag(code, StatusConstant.DEL_FLAG_NORMAL);
    }

    @Override
    public SimulateBuy findByCodeAndUser(String code, User user) {
        return this.simulateBuyRepository.findByCodeAndUserAndDelFlag(code, user, StatusConstant.DEL_FLAG_NORMAL);
    }
}